//
//  UserExperienceCell.m
//  CoolTennisBall
//
//  Created by Coollang on 16/9/6.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserExperienceCell.h"
#import "UIView+Frame.h"
#import "UserItem.h"
#import "NSString+Extension.h"

@interface UserExperienceCell ()

@property (nonatomic, strong) UIImageView *seleteImageView;
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *rightLabel;


@property (nonatomic, assign)BOOL isEdit;

@property (nonatomic, strong)UIView *lineView;
@property (nonatomic, strong) UIView *bottomLineView; // 底部分割线
@property (nonatomic, strong) UIView *topLineView; // 顶部分割线

@end

@implementation UserExperienceCell

+ (instancetype)settingViewCellTableView:(UITableView *)tableview {
    static NSString *ID = @"UserExperienceCell";
    
    UserExperienceCell *cell = [tableview dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UserExperienceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        
        [cell leftLabel];
        [cell rightLabel];
        [cell seleteImageView];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.lineView.x = kExperienceCellMargin;
    self.lineView.y = self.bounds.size.height - 1;
    self.lineView.width = self.bounds.size.width;
    self.lineView.height = 1;
    
    self.seleteImageView.x = kExperienceCellMargin;
    self.seleteImageView.centerY = self.contentView.centerY;
    if (self.isEdit) {
        
        [self.leftLabel sizeToFit];
        
        self.leftLabel.x = CGRectGetMaxX(self.seleteImageView.frame) + kExperienceCellMargin;
        self.leftLabel.centerY = self.contentView.centerY;
        
      
        self.rightLabel.x = CGRectGetMaxX(self.leftLabel.frame) + kExperienceCellMargin;
        
        self.rightLabel.centerY = self.contentView.centerY;
        self.rightLabel.width = self.contentView.width - self.rightLabel.x - kExperienceCellMargin;
        CGSize size = [self.rightLabel.text textSizeWithtextMaxSize:CGSizeMake(self.rightLabel.width, MAXFLOAT) font:self.rightLabel.font];
        self.rightLabel.height = size.height + 6;
    }else {
        
        [self.leftLabel sizeToFit];
        self.leftLabel.x = kExperienceCellMargin;
        self.leftLabel.centerY = self.contentView.centerY;
        
        self.rightLabel.x = CGRectGetMaxX(self.leftLabel.frame) + kExperienceCellMargin;
        
        self.rightLabel.width = self.contentView.width - self.rightLabel.x - kExperienceCellMargin;
        
        CGSize size = [self.rightLabel.text textSizeWithtextMaxSize:CGSizeMake(self.rightLabel.width, MAXFLOAT) font:self.rightLabel.font];
        self.rightLabel.height = size.height + 6;
        self.rightLabel.centerY = self.leftLabel.centerY;
    }
    
}

- (UIView *)lineView {
    if(_lineView == nil) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kLineBackgroundColor;
        _lineView.hidden = YES;
        [self.contentView addSubview:_lineView];
    }
    return _lineView;
}
- (UIView *)bottomLineView {
    if (_bottomLineView == nil) {
        _bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, kAutoHei(44) - 0.5, kYFScreenWidth, 0.5)];
        _bottomLineView.backgroundColor = kLineBackgroundColor;
        [self.contentView addSubview:_bottomLineView];
    }
    return _bottomLineView;
}
- (UIView *)topLineView {
    if (_topLineView == nil) {
        _topLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kYFScreenWidth, 0.5)];
        _topLineView.backgroundColor = kLineBackgroundColor;
        [self.contentView addSubview:_topLineView];
    }
    return _topLineView;
}

- (void)showLine:(BOOL)show {
    self.lineView.hidden = !show;
    self.bottomLineView.hidden = show;
}
- (void)showSectionSepTopLineView:(BOOL)show {
    self.topLineView.hidden = !show;
}

- (void)configerCellWithItem:(UserItem *)item andState:(BOOL)isEdit {
    
    self.isEdit = isEdit;
    
    if ([NSBundle currentLanguageIsChinese]) {
        self.leftLabel.text = [NSString stringWithFormat:@"%@年", item.title];
    }else {
        self.leftLabel.text = item.title;
    }
    
    
    self.rightLabel.text = item.subTitle;
    self.seleteImageView.hidden = !isEdit;
    
}
- (void)setIsSelectCell:(BOOL)isSelectCell {
    _isSelectCell = isSelectCell;
    
    self.seleteImageView.image = isSelectCell ? [UIImage imageNamed:@"user_select"] : [UIImage imageNamed:@"user_unselect"];
}

- (UIImageView *)seleteImageView {
    if (_seleteImageView == nil) {
        _seleteImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"user_unselect"]];
        _seleteImageView.bounds = CGRectMake(0, 0, 20, 20);
        _seleteImageView.hidden = YES;
        [self.contentView addSubview:_seleteImageView];
    }
    return _seleteImageView;
}

- (UILabel *)leftLabel {
    if (_leftLabel == nil) {
        _leftLabel = [[UILabel alloc] init];
        _leftLabel.font = [UIFont systemFontOfSize:16];
        _leftLabel.textColor = [UIColor colorWithHex:0x151515];
        [self.contentView addSubview:_leftLabel];
    }
    return _leftLabel;
}

- (UILabel *)rightLabel {
    if (_rightLabel == nil) {
        _rightLabel = [[UILabel alloc] init];
        _rightLabel.font = [UIFont systemFontOfSize:15];
        _rightLabel.textColor = [UIColor colorWithHex:0x151515];
        _rightLabel.numberOfLines = 0;
        [self.contentView addSubview:_rightLabel];
    }
    return _rightLabel;
}

@end
